package com.caisejingyu.whales.common.utils;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.view.MotionEvent;
import android.view.View;

public class AnimationUtil {
    /**
     * @param view     view控件
     * @param callback 回调
     */
    public static void pressAnimationListener(final View view, final OnClickListenerCallback callback) {
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    scaleAnim(view, 0.9f, 1f, 500, judgeInterior(v, event), null);
                }
                if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) {
                    scaleAnim(view, 1f, 0.9f, 500, judgeInterior(v, event), callback);
                }
                return true;
            }
        });
        
    }
    
    /**
     * 判断事件 是否在控件内部
     *
     * @param view  view控件
     * @param event 事件对象
     * @return 事件在控件内部返回true, 否则false
     */
    private static boolean judgeInterior(View view, MotionEvent event) {
        if (view == null) {
            return false;
        }
        boolean isInterior = false;
        float x = event.getRawX();
        float y = event.getRawY();
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int left = location[0];
        int top = location[1];
        int right = left + view.getMeasuredWidth();
        int bottom = top + view.getMeasuredHeight();
        if (y >= top && y <= bottom && x >= left && x <= right) {
            isInterior = true;
        }
        return isInterior;
    }
    
    /**
     * @param view       view控件
     * @param scaleMax   最大缩放比例
     * @param scaleMin   最小缩放比例
     * @param duration   持续时间
     * @param isCallback 是否回调
     * @param callback   回调
     */
    private static void scaleAnim(final View view, float scaleMax, float scaleMin, int duration, final boolean isCallback, final OnClickListenerCallback callback) {
        if (callback != null) {
            view.setEnabled(false);
        }
        //设置缩放的属性直通车
        PropertyValuesHolder scx = PropertyValuesHolder.ofFloat("scaleX", scaleMin, scaleMax);
        PropertyValuesHolder scY = PropertyValuesHolder.ofFloat("scaleY", scaleMin, scaleMax);
        ObjectAnimator objectAnimator = ObjectAnimator.ofPropertyValuesHolder(view, scx, scY).setDuration(duration);
        //动画执行的监听
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }
            
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setEnabled(true);
                if (callback != null && isCallback) {
                    callback.clickCallback(view);
                }
            }
            
            @Override
            public void onAnimationCancel(Animator animation) {
            }
            
            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        objectAnimator.start();
    }
    
    /**
     * 点击进入的回调
     */
    public interface OnClickListenerCallback {
        void clickCallback(View view);
    }
    
}
